﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NEXEntidad;
using Jayrock.Json.Conversion;
using System.Text;
using System.Text.RegularExpressions;

public partial class QueryAjax_QueryComentario : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int idLibro;
        Comentario unComentario = new Comentario();
        Usuario usuarioLogueado;
        if (Page.User.Identity.IsAuthenticated)
        {
            usuarioLogueado = ((Usuario)Session["Usuario"]);
            idLibro = Convert.ToInt32(Request.QueryString["idLibro"]);

            unComentario.Usuario = usuarioLogueado;
            unComentario.Libro = new Libro(idLibro);
            unComentario.Descripcion = Request.QueryString["Comentario"].Replace("[br]","<br>");
            unComentario.Fecha = DateTime.Now;
            unComentario.EsInsert = true;
            unComentario.Salvar();

            ComentarioCollection comentarios;
            StringBuilder sb = new StringBuilder();
            string detalle;
            string alias;
            string jsonComentarios;
            string sScript;
            comentarios = Comentario.Listar(idLibro, false);

            if (comentarios.Count > 0)
            {
                sb.Append("[");
                foreach (Comentario otroComentario in comentarios)
                {
                    sb.Append("{id: '" + otroComentario.Id + "',");
                    detalle = otroComentario.Descripcion.Replace("\"", "-");
                    detalle = detalle.Replace("\n", "<br>");
                    detalle = detalle.Replace("\r", "");
                    alias = otroComentario.Usuario.Alias.Replace("'\"", "-");
                    sb.Append("detalle: '" + detalle + "',");
                    sb.Append("alias: '" + alias + "',");
                    sb.Append("fecha: '" + ((DateTime)otroComentario.Fecha).ToString("dd/MM/yyyy") + "'");

                    sb.Append("},");
                }
                sb.Remove(sb.Length - 1, 1);
                sb.Append("]");


                jsonComentarios = JsonConvert.ExportToString(sb.ToString());
                jsonComentarios = RemoveAccentsWithRegEx(jsonComentarios);

                Response.Write(jsonComentarios);
            }
            else
            {
                Response.Write("");
            }

            Response.End();
        }

    }

    public static string RemoveAccentsWithRegEx(string inputString)
    {
        Regex replace_a_Accents = new Regex("[á|à|ä|â]", RegexOptions.Compiled);
        Regex replace_e_Accents = new Regex("[é|è|ë|ê]", RegexOptions.Compiled);
        Regex replace_i_Accents = new Regex("[í|ì|ï|î]", RegexOptions.Compiled);
        Regex replace_o_Accents = new Regex("[ó|ò|ö|ô]", RegexOptions.Compiled);
        Regex replace_u_Accents = new Regex("[ú|ù|ü|û]", RegexOptions.Compiled);
        inputString = replace_a_Accents.Replace(inputString, "a");
        inputString = replace_e_Accents.Replace(inputString, "e");
        inputString = replace_i_Accents.Replace(inputString, "i");
        inputString = replace_o_Accents.Replace(inputString, "o");
        inputString = replace_u_Accents.Replace(inputString, "u");
        return inputString;
    }
}


